import java.util.Arrays;

/**
 * @author LiuHeng
 * @date 2022-08-19 17:51
 */
public class Logarithm {
    //对数器
    public static void main(String[] args) {
        int testTime = 50000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize,maxValue);
            int[] arr2 = copyArray(arr1);
            selectSort(arr1);
            Arrays.sort(arr2);
            if (!isEqualArray(arr1,arr2)){
                System.out.println("出错啦");
                for (int j = 0; j < arr1.length; j++) {
                    System.out.println(arr1[i]);
                }
                return;
            }

        }
        System.out.println("成功了！");
    }

    //插入排序
    public static void selectSort(int[] arr){
        if (arr == null || arr.length < 2)
            return;
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j > 0 && arr[j] < arr[j-1] ; j--) {
                swap(arr,j,j-1);
            }
        }
    }

    public static void swap(int[] arr,int i, int j){
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }

    public static int[] generateRandomArray(int maxSize,int maxValue){
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random());
        }
        return arr;
    }

    public static int[] copyArray(int[] arr){
        if (arr == null)
            return null;
        int[] arr1 = new int[arr.length];
//        System.arraycopy(arr, 0, arr1, 0, arr.length);
        for (int i = 0; i < arr.length; i++) {
            arr1[i] = arr[i];
        }
        return arr1;
    }
    public static boolean isEqualArray(int[] arr1, int[] arr2){
        if (arr1 == null || arr2 == null || arr1.length != arr2.length)
            return false;
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i])
                return false;
        }
        return true;
    }
}
